package view;

import model.Video;
import javax.swing.*;
import java.awt.*;
import java.util.List;


import java.awt.event.ActionListener;


public class AdminView extends JFrame {
    private final JList<Video> videoList;  // 统一为小写开头
    private final DefaultListModel<Video> listModel;
    private final JButton addButton;
    private final JButton deleteButton;

    public AdminView() {
        setTitle("视频管理后台");
        setSize(600, 400);  // 修正参数
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        // 视频列表
        listModel = new DefaultListModel<>();
        videoList = new JList<>(listModel);
        videoList.setCellRenderer(new VideoListRenderer());
        add(new JScrollPane(videoList), BorderLayout.CENTER);  // 修正拼写

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        addButton = new JButton("添加视频");  // 修正按钮文本
        deleteButton = new JButton("删除视频");
        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    // 在AdminView.java中添加
    public DefaultListModel<Video> getVideoListModel() {
        return (DefaultListModel<Video>) videoList.getModel();
    }

    public void setVideoList(List<Video> videos) {
        listModel.clear();
        for (Video video : videos) {
            listModel.addElement(video);
        }
    }

    public Video getSelectedVideo() {
        return videoList.getSelectedValue();
    }

    public void addAddButtonListener(ActionListener listener) {
        addButton.addActionListener(listener);
    }

    public void addDeleteButtonListener(ActionListener listener) {
        deleteButton.addActionListener(listener);
    }

    // 自定义列表渲染器
    private static class VideoListRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(
                JList<?> list, Object value, int index,
                boolean isSelected, boolean cellHasFocus
        ) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Video) {  // 修正大小写
                Video video = (Video) value;
                setText(video.getTitle() + " - 上传时间: " + video.getUploadTime());
            }
            return this;
        }
    }
}
